/**
 * 
 */
package com.zc.BankCreditManagement.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.zc.BankCreditManagement.BasicDAO.BasicDAO;
import com.zc.BankCreditManagement.JavaBean.EmailVerify;


/**
 * @author ZhangCheng
 * @time 2019年9月28日
 */
public class EmailVerifyDAO extends BasicDAO<EmailVerify>{

	/**
	 * EmailVerifyDAO无参构造 重载父类构造器
	 */
	public EmailVerifyDAO() {
		super();
	}

	/**
	 * EmailVerifyDAO有参构造 重载父类构造器
	 */
	public EmailVerifyDAO(String url, String user, String pwd) {
		super(url, user, pwd);
	}
	
	/**
	 * 新增验证码(从未获取过验证码)，并插入到数据库中，参数是EmailVerify对象
	 */
	public int insertNewCode(EmailVerify eV,int validityMin) {
		int count = 0;
		String sql = "Insert into EmailVerify values(?,?,"+getCodevalidityDateSql(validityMin)+",?)";
		try {
			count = super.updateDML(sql, eV.getPeoId(), eV.getPeoVerCode() , 0);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	
	/**
	 * @apiNote 判断该用户验证码为过期
	 */
	public boolean isCodeOverDueById(int peoId) {
		boolean overDue = false;
		String sql = "SELECT * FROM emailverify WHERE peoId = ? and "+ getCodevalidityDateSql(0)+" > overduedate ";
		if(super.getInfoFromDB(sql, peoId)==null) {
			overDue = true;
		};
		return overDue;
	}
	
	/**
	 * 获取验证码过期时间
	 * @param validityMin 验证码有效期时间

	 */
	public String getCodevalidityDateSql(int validityMin) {
		return "DATE_FORMAT(DATE_ADD(NOW(),INTERVAL "+ validityMin +" MINUTE),'%Y-%m-%d %H:%i:%s')";
	}
	/**
	 * @apiNote 更新该用户验证码及过期时间
	 */
	public int updateCodeOverDueById(int peoId,String code, int validityMin) {
		int count = 0;
		String sql = "update EmailVerify set peoVerCode = ?, overDueDate =DATE_FORMAT(DATE_ADD(NOW(),INTERVAL "+validityMin +" MINUTE),'%Y-%m-%d %H:%i:%s')  where peoId = ?";
		try {
			count = super.updateDML(sql, code, peoId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	/**
	 * @apiNote 删除该用户验证码信息 isdelete置为true
	 */
	public int deleteCodeById(int peoId) {
		int count = 0;
		String sql = "update EmailVerify set isDelete = 1 where peoId = ?";
		try {
			count = super.updateDML(sql, peoId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	
	/**
	 * getEmailVerifyById(String peoId, boolean excludeDelete) 通过peoId获取验证码
	 */
	public EmailVerify getEmailVerifyById(int peoId, boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from EmailVerify where peoId = ? ");
		
		if (excludeDelete)
			sql.append("and isdelete = 0 ");// 如果排除被删除的，那么只返回未被删除的
		ArrayList<EmailVerify> m = getInfoFromDB(sql.toString(), peoId);
		if (m.size() == 0) {
			return null;
		} else {
			return m.get(0);
		}

	}
	
	@Override
	public ArrayList<EmailVerify> resultSetToArrayList(ResultSet re) {
		ArrayList<EmailVerify> eV = new ArrayList<EmailVerify>();
		
		try {
			while(re.next()) {
				eV.add(new EmailVerify(re.getInt("peoId"), re.getString("peoVerCode"), re.getDate("OverDueDate")));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return eV;
	}

}
